// server/src/utils/response.ts
import { Response } from 'express'

export interface ApiResponse<T = any> {
  success: boolean
  data?: T
  message?: string
  error?: string
  pagination?: {
    page: number
    limit: number
    total: number
    totalPages: number
  }
}

export const sendSuccess = <T>(res: Response, data?: T, message?: string): void => {
  const response: ApiResponse<T> = {
    success: true,
    data,
    message
  }
  res.json(response)
}

export const sendError = (res: Response, error: string, statusCode: number = 400): void => {
  const response: ApiResponse = {
    success: false,
    error
  }
  res.status(statusCode).json(response)
}

export const sendPagination = <T>(
  res: Response, 
  data: T[], 
  pagination: { page: number; limit: number; total: number }
): void => {
  const totalPages = Math.ceil(pagination.total / pagination.limit)
  
  const response: ApiResponse<T[]> = {
    success: true,
    data,
    pagination: {
      ...pagination,
      totalPages
    }
  }
  res.json(response)
}